﻿using System;

interface IShape
{
    double CalculateArea();  // 计算面积
    bool IsValid();          // 判断形状是否合法
}

abstract class Shape : IShape
{
    public abstract double CalculateArea();
    public abstract bool IsValid();
}

class Rectangle : Shape
{
    public double Width { get; set; }
    public double Height { get; set; }

    public Rectangle(double width, double height)
    {
        Width = width;
        Height = height;
    }

    // 计算面积
    public override double CalculateArea()
    {
        return Width * Height;
    }

    // 判断长方形是否合法
    public override bool IsValid()
    {
        return Width > 0 && Height > 0;
    }
}

class Square : Rectangle
{
    public double Side { get; set; }

    public Square(double side) : base(side, side)
    {
        Side = side;
    }

    // 正方形继承长方形的计算方式，合法性也由继承的长方形来验证
    public override double CalculateArea()
    {
        return Side * Side;
    }

    // 判断正方形是否合法
    public override bool IsValid()
    {
        return Side > 0;
    }
}

class Circle : Shape
{
    public double Radius { get; set; }

    public Circle(double radius)
    {
        Radius = radius;
    }

    // 计算面积
    public override double CalculateArea()
    {
        return Math.PI * Radius * Radius;
    }

    // 判断圆形是否合法
    public override bool IsValid()
    {
        return Radius > 0;
    }
}

class Program
{
    static void Main()
    {
        Random rand = new Random();
        IShape[] shapes = new IShape[10];
        double totalArea = 0;

        // 随机创建10个形状对象
        for (int i = 0; i < 10; i++)
        {
            int shapeType = rand.Next(1, 4);  // 随机选择形状类型 (1-长方形，2-正方形，3-圆形)
            IShape shape = null;

            switch (shapeType)
            {
                case 1: // 创建长方形
                    shape = new Rectangle(rand.Next(1, 10), rand.Next(1, 10));
                    break;
                case 2: // 创建正方形
                    shape = new Square(rand.Next(1, 10));
                    break;
                case 3: // 创建圆形
                    shape = new Circle(rand.Next(1, 10));
                    break;
            }

            if (shape != null && shape.IsValid())  // 判断形状是否合法
            {
                shapes[i] = shape;
                totalArea += shape.CalculateArea();  // 计算面积
            }
        }

        // 输出所有合法形状的面积之和
        Console.WriteLine("所有形状的面积之和: " + totalArea);
    }
}
